<?php
//信息分类控制器
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class TypeController extends CommonController
{
    public function index()
    {
        $db = \DB::table("m_type");
		//判断并封装搜索条件
		$params = array();
		if(!empty($_GET['name'])){
			$db->where("name","like","%{$_GET['name']}%");
			$params['name'] = $_GET['name'];  //维持搜索条件
		}
		$list = $db->orderBy('path', 'asc')->paginate(9);
		
		//$list = \DB::select("select * from m_type order by concat(path,id) asc");
        //处理信息
        foreach($list as &$v){
			//获取level的值
			$m = $v->level;
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
        return view("admin.type.index",['list'=>$list]);
    }
    public function create()
    {
        $list = \DB::select("select * from m_type order by concat(path,id) asc");
        //处理信息
        foreach($list as &$v){
            //获取level的值
			$m = $v->level;
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
		$f = \DB::table("m_forum")->get();
        return view("admin.type.create",['list'=>$list,'list1'=>$f]);
    }
    public function store(Request $request)
    {
		//获取要添加的数据
        $data = $request->only("name",'pid','fid');
		//执行添加
        $id = \DB::table("m_type")->insertGetId($data);
		$type = \DB::table("m_type")->where("id",$data['pid'])->first();
		$data['path'] = $type->path.$id.",";
		$data['level'] = $type->level + 1;
        \DB::table('m_type')->where('id',$id)->update($data);
        //判断
        if($id>0){
            $info = "类别信息添加成功！";
        }else{
            $info = "类别信息添加失败！";
        }
        return redirect("admin/type")->with("err",$info);
    }
    public function show($id)
    {
        //Ajax响应结果
		$g = \DB::table("m_type")->where('fid',$id)->orderBy('path', 'asc')->get();
		foreach($g as $k => $v){
			$b[$k] = ([$v->id.'=>'.$v->name, $v->id .'=>'. $v->level]);
			//$b[$k] = $v->id.'=>'.$v->name;
		}
		echo json_encode($b);
    }
    public function edit($id)
    {
        $list = \DB::select("select * from m_type order by concat(path,id) asc");
        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->path,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
		$f = \DB::table("m_forum")->get();
		$data = \DB::table("m_type")->where('id',$id)->first();
		return view("admin.type.edit",['list'=>$data,'list1'=>$f,'list2'=>$list]);
    }
    public function update(Request $request, $id)
    {
        //获取要添加的数据
        $data = $request->only("name",'pid','fid','id');
		$pid = $data['pid'];
		$id = $data['id'];
		$type = \DB::table("m_type")->where("id",$pid)->first();
		$data['path'] = $type->path.$id.",";
        \DB::table('m_type')->where('id',$id)->update($data);
        //判断
        $info = "类别信息修改成功！";
        return redirect("admin/type")->with("err",$info);
    }
    public function destroy($id)
    {
        //先判断当前类别下是否存在子类别
        $m = \DB::table('m_type')->where('pid',$id)->count();
        if($m>0){
            return back()->with("err","禁止删除");
        }  
      
        \DB::table('m_type')->delete($id);
        return redirect("admin/type")->with("err","删除成功！");
    }
}
